package top.anka1.ims;

import top.anka1.ims.ZooManagementSystem.*;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;

public class ZooManager extends JFrame {
    private ArrayList<Zoo> zoos;
    private Scanner input = new Scanner(System.in);
    private ArrayList<Logistics> logisticsList = new ArrayList<>();
    public ZooManager() {
        zoos = new ArrayList<>();
        //zoos.add(new Zoo("Southern-Zone Zoo"));
        //zoos.add(new Zoo("Northern-Zone Zoo"));
    }
    public void addZoo(Zoo zoo) {
        zoos.add(zoo);
        System.out.println("Zoo added: " + zoo.getName());
    }
    public Item createVehicle(JFrame frame) {
        Item vehicle = new Item("Transport Truck", "VEH_001");
        String input = JOptionPane.showInputDialog("Enter vehicle cost: RMB");
        try {
            vehicle.setPrice(Double.valueOf(input));
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(frame, "Invalid vehicle cost", "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }
        return vehicle;
    }
    public Item createFuel(JFrame frame) {
        Item fuel = new Item("Diesel Fuel", "FUEL_001");
        String input = JOptionPane.showInputDialog("Enter fuel cost: RMB");
        try {
            fuel.setPrice(Double.valueOf(input));
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(frame, "Invalid fuel cost", "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }
        return fuel;
    }
    public String[] createCaretakers() {
        int numCaretakers;
        // Ensure number is between 1-3
        do {
            System.out.print("Enter the number of caretakers (between 1 and 3): ");
            numCaretakers = input.nextInt();
            if (numCaretakers < 1 || numCaretakers > 3) {
                System.out.println("Invalid input! Please enter a number between 1 and 3.");
            }
        } while (numCaretakers < 1 || numCaretakers > 3);
        System.out.println("Number of caretakers set to: " + numCaretakers);
        // Get caretaker names
        String[] caretakers = new String[numCaretakers];
        input.nextLine(); // Clear buffer
        for (int i = 0; i < numCaretakers; i++) {
            System.out.println("Enter name of caretaker " + (i+1) + ": ");
            //separate caretaker names with comma
            caretakers[i] = input.nextLine();
        }
        return caretakers;
    }
    public String[] createCaretakersGUI(JFrame frame) {
        int numCaretakers;
        // Ensure number is between 1-3
        do {
            String input = JOptionPane.showInputDialog("Enter the number of caretakers (between 1 and 3):");
            if (input == null) {
                return null; // User cancelled
            }
            try {
                numCaretakers = Integer.parseInt(input);
                if (numCaretakers < 1 || numCaretakers > 3) {
                    JOptionPane.showMessageDialog(null, "Invalid input! Please enter a number between 1 and 3.");
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Invalid input! Please enter a number between 1 and 3.");
                numCaretakers = -1; // Reset to invalid state
            }
        } while (numCaretakers < 1 || numCaretakers > 3);
        System.out.println("Number of caretakers set to: " + numCaretakers);
        // Get caretaker names
        String[] caretakers = new String[numCaretakers];
        for (int i = 0; i < numCaretakers; i++) {
            caretakers[i] = JOptionPane.showInputDialog("Enter name of caretaker " + (i+1) + ":");
            if (caretakers[i] == null) {
                return null; // User cancelled
            }
        }
        return caretakers;
    }
    public Zoo getZooByName(String name) { // Get a zoo by its name
        for (Zoo zoo : zoos) {
            if (zoo.getName().equals(name)) {
                return zoo;
            }
        }
        return null; // Not found
    }
    public void addAnimal(String zooName, Animal animal) {
        Zoo zoo = getZooByName(zooName);
        if (zoo != null) {
            zoo.addAnimal(animal);
            System.out.println("Animal added to the zoo.");
        } else {
            System.out.println("Zoo not found.");
        }
    }
    public boolean moveAnimal(String fromZoo, String toZoo, String animalName) {
        JFrame frame = new JFrame();
        Zoo from = getZooByName(fromZoo);
        Zoo to = getZooByName(toZoo);
        if (from != null && to != null) {
            Logistics logistics = new Logistics(from, to, createVehicle(frame), createFuel(frame), createCaretakersGUI(frame));
            logisticsList.add(logistics);
            from.moveAnimal(animalName, to, logistics);
            return true;
        } else {
            System.out.println("One or both zoos not found.");
            return false;
        }
    }
    public ArrayList<Animal> listAnimals(String zooName) {
        Zoo zoo = getZooByName(zooName);
        if (zoo != null) {
            zoo.displayAnimals();
            return zoo.getAnimals();
        } else {
            System.out.println("Zoo not found.");
            return null;
        }
    }
    public double getTransportCost(String fromZoo, String toZoo) {
        Zoo from = getZooByName(fromZoo);
        Zoo to = getZooByName(toZoo);
        for (Logistics logistics : logisticsList) {
            if (logistics.getFromZoo().getName().equals(fromZoo) && logistics.getToZoo().getName().equals(toZoo)) {
                double cost = 0;
                cost += logistics.getTotalCost();
                return cost;
            }
        }
        System.out.println("One or both zoos not found.");
        return -1;
    }
    public ArrayList<Logistics> getLogisticsList() {
        return logisticsList;
    }
}
